import 'package:flutter/material.dart';
import '../style/color.dart';
import 'common_icon.dart';

// 顶部状态栏
class StatusAppbar extends StatelessWidget {
  StatusAppbar(
      {this.padding = const EdgeInsets.all(5),
      this.avatarSize = 44,
      this.onSearchbarTap,
      this.searchBarHeight,
      this.onAvatarTap,
      this.iconColor,
      this.onIconTap});
  final EdgeInsetsGeometry padding; // 状态栏内边距
  final VoidCallback onAvatarTap; // 点击头像触发监听函数
  final VoidCallback onIconTap; // 点击图标触发监听函数
  final VoidCallback onSearchbarTap; // 点击搜索栏触发监听函数
  final Color iconColor;// 图标颜色
  final double avatarSize; // 头像大小
  final double searchBarHeight; // 搜索栏高度

  // 构建头像方法
  ClipRRect _buildAvatar() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(avatarSize / 2),
      child: GestureDetector(
        onTap: onAvatarTap,
        child: Image.asset(
          "images/avatar.webp",
          height: avatarSize,
          width: avatarSize,
        ),
      ),
    );
  }

  // 构建搜索栏方法
  Padding _buildSearchBar() {
    return Padding(
        padding: EdgeInsets.symmetric(horizontal: 15), // 搜索栏左右15边距
        child: GestureDetector(
          onTap: onSearchbarTap,
          child: Container(
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.all(5),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(16), color: lightGrayTextColor),
            height: searchBarHeight,
            child: CommonIcon(
              Icons.search,
              margin: const EdgeInsets.fromLTRB(10, 5, 0, 5),
              height: 32,
              iconColor: grayTextColor,
            ),
          ),
        ));
  }

  @override
  Widget build(BuildContext statusAppbarCtx) {
    // 如果首次请求则计算手机顶部间距
    // if (phoneTopPadding == null) {
    //   phoneTopPadding = MediaQuery.of(statusAppbarCtx).padding.top;
    // }
    return Container(
      // margin: EdgeInsets.only(top: phoneTopPadding),// 异形屏兼容 
      padding: padding,
      child: Row(
        children: <Widget>[
          _buildAvatar(),
          Expanded(child: _buildSearchBar()),
          CommonIcon(
            Icons.games,
            padding: EdgeInsets.symmetric(horizontal: 10),
            iconColor: iconColor,
            onIconTap: onIconTap,
          ),
          CommonIcon(
            Icons.mail_outline,
            padding: EdgeInsets.symmetric(horizontal: 10),
            iconColor: iconColor,
            onIconTap: onIconTap,
          )
        ],
      ),
    );
  }
}
